package com.simple.slider.puzzle;


import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;

public class SimpleSliderPuzzleActivity extends Activity {
	private static final String LOG_TAG = SimpleSliderPuzzleActivity.class.getName();
	
	private final int clickThresholdTime = 200; //ms
	
	// could be value obtained from a UI input or saved preferences
	private int puzzleSize = 4;
	
	private PuzzleView puzzleView;
	private Animation fadeIn;
	
	private AnimationListener puzzleCompleteAnimationListener = new AnimationListener() {

		@Override
		public void onAnimationEnd(Animation animation) {
			// when fade out animation is done, start the fade in
			puzzleView.setCompleteImage();
			puzzleView.startAnimation(fadeIn);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			
		}

		@Override
		public void onAnimationStart(Animation animation) {
			
		}		
	};
	    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(LOG_TAG, "onCreate");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.slide_puzzle);
        puzzleView = (PuzzleView) findViewById(R.id.puzzle_tile_grid);
        
        fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        
        if (savedInstanceState != null) {
	        Parcelable[] boardState = savedInstanceState.getParcelableArray("board");
			SliderTile[] savedTileArray = null;
			if (boardState != null) {
				int len = boardState.length;
				
				savedTileArray = new SliderTile[len];
				for (int i = 0; i < len; i++) {
					savedTileArray[i] = (SliderTile) boardState[i];
				}
			}
			
			puzzleView.startNewGame(puzzleSize, savedTileArray, R.drawable.us);
        } else {
        	puzzleView.startNewGame(puzzleSize, null, R.drawable.us);
        }  
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	Log.i(LOG_TAG, "onResume");  	
    }
    
    @Override
    public void onPause() {
        super.onPause();
        Log.i(LOG_TAG, "onPause");
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "onDestroy");
    }

    @Override 
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);

        int action = event.getAction();
        float firstDownEvent = event.getDownTime();
        //Log.i(LOG_TAG,  "onTouchEvent firstDownEvent: " + firstDownEvent);
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
            	puzzleView.chooseTile(event.getX(), event.getY());
            	return true;
            }
            case MotionEvent.ACTION_MOVE: {
            	puzzleView.dragTile(event.getX(), event.getY());
            	return true;
            }
            case MotionEvent.ACTION_UP: {
              	float firstUpEvent = event.getEventTime();
            	//Log.i(LOG_TAG,  "onTouchEvent difference: " + diff);
            	if((firstUpEvent - firstDownEvent) < clickThresholdTime) {
            		puzzleView.clickTile(event.getX(), event.getY());
            	} else {
            		puzzleView.dropTile(event.getX(), event.getY());
            	}
            	showImageIfPuzzleSolved();
            	return true;
            }
        }
        
        // touch event was not consumed, let it be handled elsewhere
        return false;
    }
    
    public void showImageIfPuzzleSolved() {
	    if (puzzleView.getPuzzleBoard().getPuzzleSolvedState() == false && puzzleView.getPuzzleBoard().checkPuzzleSolved()) {
	    	Animation fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
	    	fadeOut.setAnimationListener(puzzleCompleteAnimationListener);
	    	puzzleView.startAnimation(fadeOut);	
	    }
    }
    
//    @Override
//    public void onConfigurationChanged(Configuration newConfig) {
//    	super.onConfigurationChanged(newConfig); 
//    	if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT ) {
//    		Log.i(LOG_TAG,  "ORIENTATION_PORTRAIT");
//    	} else if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ){
//    		Log.i(LOG_TAG,  "ORIENTATION_LANDSCAPE");
//    	}
//    }

    
    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.i(LOG_TAG, "onSAVEInstanceState");
        
        savedInstanceState.putParcelableArray("board", puzzleView.getPuzzleBoard().serializeBoard());
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	Log.i(LOG_TAG, "onRESTOREInstanceState");
    }
    
}